package com.zmovie.admin.controller.movie;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zmovie.admin.constant.Constants;
import com.zmovie.admin.controller.vo.MovieCommentVO;
import com.zmovie.common.error.BusinessException;
import com.zmovie.common.error.EnumBusinessError;
import com.zmovie.common.response.CommonReturnType;
import com.zmovie.framework.domain.Movie;
import com.zmovie.framework.domain.MovieComment;
import com.zmovie.framework.domain.User;
import com.zmovie.framework.service.MovieCommentService;
import com.zmovie.framework.service.MovieService;
import com.zmovie.framework.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 挚爱之夕
 * @version 1.0
 * @implSpec 电影评论模块
 * @since 2023-11-30 22:01
 */
@Controller
@Api(tags = "电影评论模块")
@RequestMapping("/movie/comment")
public class MovieCommentController {
    @Autowired
    private MovieCommentService movieCommentService;
    @Autowired
    private UserService userService;
    @Autowired
    private MovieService movieService;

    @GetMapping
    public String index(Model model) {
        Page<MovieComment> commentPage =
                movieCommentService.pageMovieComment(new MovieComment(),
                        Integer.valueOf(Constants.PAGE_NO),
                        Integer.valueOf(Constants.PAGE_SIZE));
        //查电影
        List<Movie> movies = movieService.list();
        model.addAttribute(Constants.MOVIES_KEY, movies);
        //转VO
        model.addAttribute(Constants.COMMENT_PAGE_KEY, convert(commentPage));
        return "movie/movie_comment";
    }

    @GetMapping("/{id}")
    @ResponseBody
    @ApiOperation("根据id获取影评")
    public Object getMovieComment(@PathVariable("id") Integer id) {
        return CommonReturnType.success(movieCommentService.getById(id));
    }

    @PostMapping
    @ResponseBody
    @ApiOperation("添加影评")
    public Object addMovieComment(@Validated MovieComment movieComment, HttpSession session) {
        //设置评论时间
        movieComment.setCommentTime(new Date());
        //获取当前用户信息
        User user = (User) session.getAttribute(Constants.SESSION_USER_KEY);
        if(user != null)
            movieComment.setUserId(user.getId());
        movieCommentService.save(movieComment);
        return CommonReturnType.success("添加成功!");
    }

    @DeleteMapping("/{id}")
    @ResponseBody
    @ApiOperation("删除影评")
    public Object deleteMovieComment(@PathVariable("id") @NotNull Integer id) {
        movieCommentService.removeById(id);
        return CommonReturnType.success("删除成功!");
    }

    @DeleteMapping
    @ResponseBody
    @ApiOperation("批量删除影评")
    public Object deleteMovieCommentBatch(Integer[] id) throws BusinessException {
        //参数校验
        if (id == null || id.length == 0) {
            throw new BusinessException(EnumBusinessError.PARAMETER_VALIDATION_ERROR);
        }
        movieCommentService.removeByIds(Arrays.asList(id));
        return CommonReturnType.success("批量删除成功!");
    }

    @PostMapping("/search")
    @ApiOperation("搜索影评")
    public String search(MovieComment movieComment,
                         Integer pageNo,
                         @RequestParam(value = "pageSize", required = false, defaultValue = Constants.PAGE_SIZE) Integer pageSize,
                         Model model) throws BusinessException {
        if(pageNo == null){
            throw new BusinessException(EnumBusinessError.PARAMETER_VALIDATION_ERROR);
        }
        Page<MovieComment> commentPage = movieCommentService.pageMovieComment(movieComment, pageNo, pageSize);
        model.addAttribute(Constants.COMMENT_PAGE_KEY, convert(commentPage));
        return "movie/movie_comment :: commentList";
    }

    private MovieCommentVO convert(MovieComment movieComment) {
        //查所属用户
        User user = userService.getById(movieComment.getUserId());
        //查所属电影
        Movie movie = movieService.getById(movieComment.getMovieId());
        MovieCommentVO movieCommentVO = new MovieCommentVO();
        BeanUtils.copyProperties(movieComment, movieCommentVO);
        movieCommentVO.setUser(user);
        movieCommentVO.setMovie(movie);
        return movieCommentVO;
    }

    private Page<MovieCommentVO> convert(Page<MovieComment> commentPage) {
        Page<MovieCommentVO> movieCommentVOPage = new Page<>();
        BeanUtils.copyProperties(commentPage, movieCommentVOPage);
        List<MovieCommentVO> commentVOList = commentPage.getRecords().stream().map(this::convert).collect(Collectors.toList());
        movieCommentVOPage.setRecords(commentVOList);
        return movieCommentVOPage;
    }
}
